import {
  chemicalAlias
} from '../../dataSource'
import {
  request
} from '../../api/index'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      value: false,
      observer(res){
        if(res){
          this.getFactor();
        }
      }
    },
    dataSource: {
      type: Object,
      value: {},
      observer: 'setDataSource'
    },
    enable: {
      type: Boolean,
      value: false,
      observer: 'checkEnable'
    },
    root: {
      type: Boolean,
      value: true
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    list: [],
    correctionAmount: {
      name: 'expansionFactor',
      text: '修正量',
      elementType: 'correctionAmount',
      value: '',
      label: '膨胀因子'
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onConfirm(arg) {
      this.triggerEvent('onConfirm', arg)
    },
    checkEnable(data) {

    },
    onClose() {
      this.triggerEvent('onClose')
    },
    onSubmit(event) {
      return 
      const target = event.detail.value;
 
      // 检测是否全部填了
      const nill = Object.keys(target).some(item => {
        return target[item] === ""
      })
      if (nill) {
        wx.showToast({
          title: '存在项目未填',
          icon: 'error'
        })
        return false
      }
      let obj = {};
      for (let it of Object.keys(target)) {
        const keys = it.split('.');
        if (obj[keys[0]] === undefined) {
          obj[keys[0]] = {
            [keys[1]]: target[it]
          }
        } else {
          obj[keys[0]][keys[1]] = target[it]
        }
      }
      this.onConfirm(obj);
  
    },
    setDataSource() {
      const list = Object.keys(chemicalAlias);
      const arr = [],
        dataSource = this.properties.dataSource;
      this.setData({
        correctionAmount: {
          name: 'expansionFactor',
          text: '修正量',
          elementType: 'correctionAmount',
          value: dataSource?.correctionAmount?.expansionFactor,
          label: '膨胀因子'
        }
      })
      for (let key of list) {
        if (key === 'name' || key === 'lossOnIgnition') {
          continue
        } else {
          arr.push({
            name: chemicalAlias[key],
            value: key,
            values: {
              expansionFactor: dataSource?.[key]?.expansionFactor || '',
              fluxFactor: dataSource?.[key]?.fluxFactor || '',
              tensionFactor: dataSource?.[key]?.tensionFactor || '',
            }
          })
        }
      }
      this.setData({
        list: arr
      })
    },
    /**
     * 
     * @param {*} source 源 Array
     * @param {*} key 
     * @param {*} target 
     */
    findItem(source, key, target) {
      const result = source.find(it => it['elementType'] === key);
      if(result) return result[target]
      return undefined
    },
    /**
     * 查询因子
     */
    getFactor() {
      request.getFactor({}, res => {
        const list = Object.keys(chemicalAlias);
        const arr = [],
          dataSource = res[0].factorDatas;
   
        this.setData({
          correctionAmount: {
            name: 'expansionFactor',
            text: '修正量',
            elementType: 'correctionAmount',
            value: dataSource.find(it => it.elementType === 'correctionAmount')?.expansionFactor,
            label: '膨胀因子'
          }
        })
        for (let key of list) {
          if (key === 'name' || key === 'lossOnIgnition') {
            continue
          } else {
            arr.push({
              name: chemicalAlias[key],
              value: key,
              values: {
                expansionFactor: this.findItem(dataSource, key, 'expansionFactor'),
                fluxFactor: this.findItem(dataSource, key,'fluxFactor'),
                tensionFactor:  this.findItem(dataSource, key, 'tensionFactor'),
              }
            })
          }
        }
        this.setData({
          list: arr
        })
         this.triggerResult(arr);
      })
    },
    triggerResult(arr){
       let obj = {};
       arr.map(it=>{
         obj[it.value] = it.values
       })
       obj['correctionAmount'] = this.data.correctionAmount.expansionFactor;
       this.triggerEvent('onConfirm', obj);
    }
  },
  lifetimes: {
    attached: function () {
      // 在组件实例进入页面节点树时执行
     // this.setDataSource();
     this.getFactor();

    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },
  },
})